#!/usr/bin/env bash
# @sys: centos-stream-8
# @date: Mon Jun 27 09:27:42 CST 2022
# @author: yangfan
# @email: yangfan@moonpac.com
# @info: install-nginx
# @run: ./install-nginx.sh 1 1.20.2 /opt



# vars
if [ ! -n "${1}" ] ;then
    echo -e "\033[31m Please enter parameter online (1) or offline (2) install --------> fail\033[0m"; exit 1;
else
    model=${1}
fi

if [ ! -n "${2}" ] ;then
    echo -e "\033[31m Please enter parameter nginx version (1.20.2, 1.21.6) --------> fail\033[0m"; exit 1;
else
    version=${2}
fi

if [ ! -n "${3}" ] ;then
    echo -e "\033[32mDefault install dir '/opt' \033[0m";
    install_path=/opt
else
    install_path=${3}
fi


# fun
function check() {
    if [[ $? == 0 ]]; then
        echo -e "\033[32m${1} --------> success\033[0m";
    else
        echo -e "\033[31m${1} --------> fail\033[0m";
        exit 1;
    fi
}

function wgetFun() {
    wget -c ${1} &>/dev/null
    check "wget ${1}"
}

function yumFun() {
    yum install ${*} -y &>/dev/null
    check "yum install ${*}"
}

function addUser() {
    id ${1} &>/dev/null
    if [[ $? != 0 ]]; then
        useradd -M -s /sbin/nologin ${1}
        check "add ${1} user"
    else
        id ${1}
    fi
}

function addFilewall() {
    status=`firewall-cmd --query-port=${1}`
    if [[ ${status} == no ]]; then
        firewall-cmd --add-port=${1} &>/dev/null
        firewall-cmd --add-port=${1} --permanent &>/dev/null
        check "firewall add ${1}"
    else
        echo "firewall add ${1}"
    fi
}

function startService() {
    systemctl enable ${1}.service --now &>/dev/null
    systemctl restart ${1}.service &>/dev/null
    check "restart ${1}"
}


# init soft install
yumFun gcc gcc-c++ make cmake git wget tar zip unzip bzip2 bash-completion tmux


# rely install
yumFun pcre-devel zlib-devel openssl-devel


# download nginx
if [[ ${model} == 1 ]]; then
    wgetFun https://nginx.org/download/nginx-${version}.tar.gz
elif [[ ${model} == 2 ]]; then
    if [[ ! -f nginx-${version}.tar.gz ]]; then
        echo "source nginx-${version}.tar.gz not exist"
        exit 1;
    fi
fi


# unzip nginx
tar xf nginx-${version}.tar.gz && cd nginx-${version} || exit 1;

# configure && make && make install nginx
./configure --prefix=${install_path}/nginx-${version} \
--user=nginx --group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_random_index_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream &>/dev/null && check "configure nginx-${version}"

make -j12 &>/dev/null && check "make nginx-${version}"
make install &>/dev/null && check "make install nginx-${version}"
cd .. && rm -rf nginx-${version}*


# addFilewall and user
addFilewall 80/tcp
addUser nginx


# nginx service
cat > /lib/systemd/system/nginx-${version}.service << END
[Unit]
Description=nginx ${version} service
After=network.target network-online.target syslog.target
Wants=network.target network-online.target

[Service]
Type=forking
ExecStart=${install_path}/nginx-${version}/sbin/nginx
ExecReload=${install_path}/nginx-${version}/sbin/nginx -s reload
ExecStop=${install_path}/nginx-${version}/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
END


# chown
chown nginx:nginx -R ${install_path}/nginx-${version}


# conf
if [[ -f nginx.conf ]]; then
    cat nginx.conf > ${install_path}/nginx-${version}/conf/nginx.conf
fi

sed -i 's/nginx\/$nginx_version/nginx/' ${install_path}/nginx-${version}/conf/fastcgi.conf


# 开机自启动并立即启动
startService nginx-${version}
